/**
 *
 * @author ilya portnyagin iportnyagin@gmail.com
 */

package ru.portnyagin.helpdeskru.service;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ru.portnyagin.helpdeskru.model.HistoryRequest;
import ru.portnyagin.helpdeskru.model.HistoryRequest_;
import ru.portnyagin.helpdeskru.model.Request;

@Stateless
public class HistoryRequestService extends AbstractService<HistoryRequest> {
    @PersistenceContext(unitName = "HelpDeskRuPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public HistoryRequestService() {
        super(HistoryRequest.class);
    }
    
    public List<HistoryRequest> find(Request request) {
        if (request == null) {
            return null;
        }
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery cq = cb.createQuery();
        Root<HistoryRequest> h = cq.from(HistoryRequest.class);
        cq.where(cb.equal(h.get(HistoryRequest_.request), request));
        cq.orderBy(cb.desc(h.get(HistoryRequest_.modifiedDate)));
        
        cq.select(h);

        return em.createQuery(cq).getResultList();
    }
        
            
            
        
}
